/**
 * 
 */
package org.aman.sprsecurity.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aman.sprsecurity.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * @Copyright: 本内容仅限于重庆爱赢科技有限公司内部使用，禁止转发. 
 * @Author: daijiangguo 2014-6-11 上午11:26:17 
 * @Version: $Id$
 * @Desc: <p></p>
 */
@Component
public class UserManager implements UserDetailsService, AuthenticationSuccessHandler{

    public Map<String, User> userDb = new HashMap<String, User>();
    {
        userDb.put("aman0", new User("001", "aman0", "0aa"));
        userDb.put("aman1", new User("002", "aman1", "1"));
        userDb.put("aman2", new User("003", "aman2", "2"));
        userDb.put("aman3", new User("004", "aman3", "3"));
        userDb.put("aman4", new User("005", "aman4", "4"));
        userDb.put("aman5", new User("006", "aman5", "5"));
        userDb.put("aman6", new User("007", "aman6", "6"));
        userDb.put("aman7", new User("008", "aman7", "7"));
        userDb.put("aman8", new User("009", "aman8", "8"));
        userDb.put("aman9", new User("010", "aman9", "9"));
    }
    
    public User findUserByUserName(String userName){
        return userDb.get(userName);
    }

    /* (non-Javadoc)
     * @see org.springframework.security.web.authentication.AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
     */
    public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication arg2)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        System.out.println("=============onAuthenticationSuccess");
        arg1.sendRedirect("list.do");
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
     */
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = this.findUserByUserName(userName);
        return user;
    }
}
